package space.enemy;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;



public class SpaceEnemy2 extends AnimatedSprite {    	
	
	public static final int HEALTH = 100;    	
	public static final int DAMAGE = 20;
	public static final int LOW_HEALTH = DAMAGE;
	public static final float SPEED = 10.0f; 
	

	
    protected float mSpeed;
    protected boolean mAlive;
    protected int mLife;
    public int getmLife() {
		return mLife;
	}

	public void setmLife(int mLife) {
		this.mLife = mLife;
	}


	protected PhysicsHandler physicsHandler;
	private Texture mTextureAlieni;
	
	private SpaceEnemyController sec;
	private TiledTextureRegion mAlieniTextureRegion;
    public Texture getmTextureAlieni() {
		return mTextureAlieni;
	}

	public void setmTextureAlieni(Texture mTextureAlieni) {
		this.mTextureAlieni = mTextureAlieni;
	}

	public TiledTextureRegion getmAlieniTextureRegion() {
		return mAlieniTextureRegion;
	}

	public void setmAlieniTextureRegion(TiledTextureRegion mAlieniTextureRegion) {
		this.mAlieniTextureRegion = mAlieniTextureRegion;
	}


	private static int mMoveDir;

      public SpaceEnemy2(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final int pLife, final Scene pScene,float speed) {
    	super(pX, pY, pTextureRegion);
    	//this.animate(new long[] { 100, 100, 100 ,100}, 0, 3, true); 
    	this.animate(new long[] { 100, 100}, 0, 1, true);  
    	
    	//mTextureRegion = pTextureRegion;
		mLife = pLife;	
		mSpeed = speed;
		mMoveDir = 1;		
		mAlive = true;
		physicsHandler = new PhysicsHandler(this);
		this.setVisible(false);
		this.registerUpdateHandler(physicsHandler);
		
		pScene.getLastChild().attachChild(this);
    }
      
      public SpaceEnemy2(final float pX, final float pY, final int pLife, final Scene pScene,float speed,Texture pTextureAlieni, TiledTextureRegion mAlieniTextureRegion,SpaceEnemyController sec) {
    	  
    	  
    	  super(pX, pY, mAlieniTextureRegion);
    	  this.mTextureAlieni = pTextureAlieni;
    	  this.sec = sec;
  		//this.mAlieniTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTextureAlieni, this, "gfx/prova_alieni.png", 0, 0, 4, 1);
  		this.mAlieniTextureRegion = mAlieniTextureRegion;
    	  
    	
      	//this.animate(new long[] { 100, 100, 100 ,100}, 0, 3, true); 
      	this.animate(new long[] { 100, 100}, 0, 1, true);  
      	
      	//mTextureRegion = pTextureRegion;
  		mLife = pLife;	
  		mSpeed = speed;
  		mMoveDir = 1;		
  		mAlive = true;
  		physicsHandler = new PhysicsHandler(this);
  		this.registerUpdateHandler(physicsHandler);
  		
  		pScene.getLastChild().attachChild(this);
      }
    
      public static void setMoveLeft() {
        mMoveDir = -1;
    }
      public static void setMoveRight() {
        mMoveDir = 1;
    }

    // ===========================================================
    // Class Methods
    // ===========================================================
    
    
      public void update(final boolean pMoveDown) {
        if (mLife <= 0) {
            mAlive = false;
        }
        else {
        	if ( pMoveDown == true ) {
        		this.setPosition(this.getX(), this.getY() + 10);
        	}            
        	physicsHandler.setVelocity(this.mSpeed * this.mMoveDir, 0);
        }             
    }
    
     public void decreaseLife(int pAmount) {
    	mLife -= pAmount;
    	if (mLife == SpaceEnemy2.LOW_HEALTH) {
    		
    		this.setColor(1.0f, 0, 0, this.getAlpha());
    		//mTextureAlieni.clearTextureSources();
    		//TextureRegionFactory.createTiledFromAsset(this.mTextureAlieni,sec , "gfx/alieni_rotti.png", 0, 0, 2, 1);
    	}
    	else if (mLife <= 0) {
    		mAlive = false;
    	}
    }
    
    public boolean isAlive() {
    	return mAlive;
    }     
    
    public void increaseSpeed() {
    	mSpeed += 5.0f;
    }
    

	public void halt() {
		physicsHandler.setVelocity(0, 0);
		this.stopAnimation();
	}
               
}